<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Collage;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CollageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Collage(), function (Grid $grid) {
            $grid->addTableClass(['table-text-center']);
            $grid->column('id')->sortable();
            $grid->column('cover')->image('',80);
            $grid->column('title');
            $grid->column('houses_name');
            $grid->column('houses_address');
            $grid->column('time','起始时间')->display(function () {
                return $this->start_time . '-' . $this->end_time;
            });
            $grid->column('intermediary_id','发起人');
            $grid->column('collage_status','开团状态')->display(function () {
                if ($this->status == 1){
                    return "<span class='badge badge-success'>已开团</span>";
                }else{
                    return '<span class="badge badge-secondary">未开团</span>';
                }
            });
            $grid->column('status','状态')->switch();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->equal('title','团购名称')->width(3);
                $filter->equal('status','状态')->select([1 => '开启', 0 => '关闭'])->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Collage(), function (Show $show) {
            $show->field('id');
            $show->field('cover');
            $show->field('title');
            $show->field('houses_name');
            $show->field('houses_address');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('desc');
            $show->field('intermediary_id');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Collage(), function (Form $form) {
            $form->display('id');
            $form->file('cover')->autoUpload()->uniqueName()->saveFullUrl();
            $form->text('title');
            $form->text('houses_name');
            $form->text('houses_address');
            $form->datetime('start_time');
            $form->datetime('end_time');
            $form->textarea('desc');
            $form->text('intermediary_id');
            $form->switch('status','状态');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
